/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.admin.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.admin.entity.BusinessApprove;
import org.springblade.modules.admin.enums.ApproveStatusEnum;
import org.springblade.modules.admin.enums.ApproveTypeEnum;
import org.springblade.modules.admin.service.approve.strategy.ApproveBusinessFlow;
import org.springblade.modules.admin.service.approve.strategy.ApproveBusinessStrategy;
import org.springblade.modules.admin.vo.BusinessApproveVO;
import org.springblade.modules.admin.mapper.BusinessApproveMapper;
import org.springblade.modules.admin.service.IBusinessApproveService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.modules.admin.vo.PlanApproveUserVO;
import org.springblade.modules.admin.vo.PlanApproveVO;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 审批流程表 服务实现类
 *
 * @author zcc
 * @since 2021-12-07
 */
@Service
public class BusinessApproveServiceImpl extends BaseServiceImpl<BusinessApproveMapper, BusinessApprove> implements IBusinessApproveService {

	@Override
	public IPage<BusinessApproveVO> selectBusinessApprovePage(IPage<BusinessApproveVO> page, BusinessApproveVO businessApprove) {
		return page.setRecords(baseMapper.selectBusinessApprovePage(page, businessApprove));
	}

	@Override
	public Object detail(Long businessApproveId) {
		ApproveBusinessFlow businessFlow = ApproveBusinessStrategy.get(ApproveTypeEnum.DATA_ALTER.getId());
		return businessFlow.detail(businessApproveId);
	}

	@Override
	public Integer myApproveTotal() {
		int total = baseMapper.selectCount(Wrappers.<BusinessApprove>lambdaQuery().eq(BusinessApprove :: getApproveStatus, ApproveStatusEnum.PENDING.getId()).eq(BusinessApprove :: getIsDeleted, 0));
		return total;
	}

	@Override
	public boolean approve(BusinessApproveVO businessApprove) {
		ApproveBusinessFlow businessFlow = ApproveBusinessStrategy.get(ApproveTypeEnum.DATA_ALTER.getId());
		return businessFlow.approve(businessApprove);
	}


	@Override
	public PlanApproveVO malfunctionApproveStatistical(Long userId) {
		PlanApproveVO malfunctionApprove = new PlanApproveVO();
		List<BusinessApproveVO> businessApproveList = baseMapper.malfunctionApproveList(userId);
		if (Func.isNull(businessApproveList)) {
			malfunctionApprove.setTotal(0L);
			malfunctionApprove.setApproveCount(0L);
			return malfunctionApprove;
		}

		// 封装出有且仅有自己是唯一审核人的数据
		List<PlanApproveUserVO> approveUserList = businessApproveList.stream().map(t -> {
			PlanApproveUserVO approveUser = new PlanApproveUserVO();
			approveUser.setPlanId(t.getWorkOrderId());
			// 过滤出在职人员数量
			approveUser.setApproveUserCount(t.getApproveUserList().stream().filter(p -> p.getStatus() == 1).count());
			return approveUser;
		}).filter(t -> t.getApproveUserCount() < 2).collect(Collectors.toList());

		malfunctionApprove.setTotal(approveUserList.stream().count());
		malfunctionApprove.setApproveCount(approveUserList.stream().count());
		malfunctionApprove.setPlanIds(Func.isEmpty(approveUserList) ? null : approveUserList.stream().map(t -> t.getPlanId()).collect(Collectors.toList()));

		return malfunctionApprove;
	}

	@Override
	public BusinessApprove getByWorkOrderId(Long workOrderId) {
        return this.getOne(Wrappers.<BusinessApprove>lambdaQuery().eq(BusinessApprove :: getWorkOrderId, workOrderId), false);
	}
}
